﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace DataAccess
{
    public class InsuranceTypeAccess
    {
        public string InsertInsuranceType(InsuranceTypeInfo insuType)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var insu = db.InsuranceTypes.Where(i => i.InsuranceName.Equals(insuType.InsuranceName)).SingleOrDefault();
            if (insu == null)
            {
                InsuranceType i = new InsuranceType();
                i.InsuranceName = insuType.InsuranceName;
                i.Description = insuType.Description;
                i.UnitPrice = insuType.UnitPrice;

                db.InsuranceTypes.InsertOnSubmit(i);
                db.SubmitChanges();
                return "Insert successfull";
            }
            else
            {
                return "Insurance name is really exist";
            }
            
        }

        public List<InsuranceTypeInfo> getListOfInsuranceType()
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var result = from i in db.InsuranceTypes
                         orderby i.InsuranceName
                         select new
                         InsuranceTypeInfo
                         {
                             InsuranceID = i.InsuranceID,
                             InsuranceName = i.InsuranceName,
                             Description = i.Description,
                             UnitPrice = Convert.ToSingle(i.UnitPrice) 
                         };
            return result.ToList();
        }

        public InsuranceTypeInfo getInsuranceByName(string insuranceName)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var insu = db.InsuranceTypes.Where(i => i.InsuranceName.Equals(insuranceName)).SingleOrDefault();

            if (insu != null)
            {
                InsuranceTypeInfo i = new InsuranceTypeInfo();
                i.InsuranceID = insu.InsuranceID;
                i.InsuranceName = insu.InsuranceName;
                i.Description = insu.Description;
                i.UnitPrice = Convert.ToSingle(insu.UnitPrice);

                return i;
            }
            else
            {
                return null;
            }
        }

        public InsuranceTypeInfo getInsuranceByID(int insuID)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var insu = db.InsuranceTypes.Where(i => i.InsuranceID.Equals(insuID)).SingleOrDefault();

            if (insu != null)
            {
                InsuranceTypeInfo i = new InsuranceTypeInfo();
                i.InsuranceID = insu.InsuranceID;
                i.InsuranceName = insu.InsuranceName;
                i.Description = insu.Description;
                i.UnitPrice = Convert.ToSingle(insu.UnitPrice);

                return i;
            }
            else
            {
                return null;
            }
        }
    }
}
